const path = require( 'path' );
const express = require( 'express' );

const port = 8080 ;
const app = express();

// 处理静态资源
const staticRoot = path.resolve( 'public' );
const staticHandler = express.static( staticRoot );
app.use( staticHandler );

// 处理POST方式提交的application/x-www-form-urlencode表单
app.use( express.urlencoded( { extended: false } ) );

app.use( '/sign/in' , (req,resp)=>{
    console.log( req.method , req.originalUrl );
    resp.send( '<h3>登录</h3>' );
});

app.use( '/sign/up' , (req,resp,next)=>{
    console.log( req.method , req.originalUrl );
    next(); // 若同一个路径对应多个中间件函数，则需要调用next向后传递请求
});

// "路由" 由 请求方式( http method ) 和 请求路径( path ) 组成
app.get( '/sign/up' , (req,resp)=>{
    console.log( '用GET方式请求' , req.originalUrl );
    resp.send( '<h3>注册页面</h3>' );
});

app.post( '/sign/up' , (req,resp)=>{
    console.log( '用POST方式请求' , req.originalUrl );

    let p = req.body ;
    if( p.username && p.password && p.password === p.confirm ){
        resp.send( '<h3>注册成功</h3>' );
        return ;
    }

    resp.send( '<h3>注册失败</h3>' );
});

app.listen( port );